package com.fingard.dsp.bank.reqrespobj.downloadreceipt;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.dsp.bank.reqrespobj.RequestBase;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.detail.ReqQryReceiptDetatil;
import com.fingard.xml.XmlTextReader;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;

public class ReqQryReceipt extends RequestBase {
    /*
    <ATSYH>
      <TransReq>
        <BankCode></BankCode>
        <TransType>DownloadReceipt</TransType>
        <Entity>5000001</Entity>
        <TransSeqID>20171114111025484</TransSeqID>
        <TransTime>2017-11-14 11:10:25</TransTime>
        <TransParam>
          <DetailRecord>
            <ActNum>103330101</ActNum>
            <AreaCode>3301</AreaCode>
            <AreaName>浙江省_杭州市</AreaName>
            <DateFrom>2017-06-28</DateFrom>
            <DateTo>2017-06-28</DateTo>
            <DirectPayWay></DirectPayWay>
            <ReqBatchNO></ReqBatchNO>
            <AskNumberList></AskNumberList>
          </DetailRecord>
        </TransParam>
      </TransReq>
    </ATSYH>
    */
    /**
     * 账号
     */
    public String actNum = "";
    public String areaCode = "";
    public String areaName = "";
    public String dateFrom = "";
    public String dateTo = "";
    public String directPayWay = "";
    public String reqBatchNo = "";
    public String askNumberList = "";

    public String qryType = "";//<!--查询类型:1:T+0  2:T+1-->
    public String billType = "";//<!--回单类型char(1)，0：全部；1：转账类；2：利息类；3：收费类；4：电子缴税；5：网银结售汇；6：现金管理转账-->

    /**币种*/
    public String actCur = "";
    private XmlTextReader xmlReader;
    /**电子回单编号*/
    public String receiptNo;//电子回单编号
    
    /**主管组织，用于回单保存路径的设置，即不同主管组织保存到不同目录*/
    public String cmptOrg;

    /**是否需要返回回单核对数据*/
    public Boolean needChkData;
    //直联账户权限
    public HashSet<String> directRight;

    public ReqQryReceiptDetatil readedDetail;

    public boolean readRecord() throws Exception {
        readedDetail = null;
        if (xmlReader.read(PATH_Detail)) {
            readedDetail = new ReqQryReceiptDetatil(xmlReader.readedXElement);
            return true;
        } else {
            return false;
        }
    }

    public ReqQryReceipt(XmlTextReader p_xmlReader, ReqRespHeader p_header) throws Exception {
        reqHeader = p_header;
        xmlReader = p_xmlReader;

        actNum = xmlReader.getSimpleText("ActNum");
        areaCode = xmlReader.getSimpleText("AreaCode");
        areaName = xmlReader.getSimpleText("AreaName");
        dateFrom = xmlReader.getSimpleText("DateFrom");
        dateTo = xmlReader.getSimpleText("DateTo");
        directPayWay = xmlReader.getSimpleText("DirectPayWay");
        reqBatchNo = xmlReader.getSimpleText("ReqBatchNo");
        askNumberList = xmlReader.getSimpleText("AskNumberList");
        qryType = xmlReader.getSimpleText("QryType");
        billType = xmlReader.getSimpleText("BillType");
        actCur = xmlReader.getSimpleText("ActCur");
        receiptNo = xmlReader.getSimpleText("ReceiptNo");
        cmptOrg = xmlReader.getSimpleText("CmptOrg");
        String reqNeedChkData = xmlReader.getSimpleText("NeedChkData");
        if ("1".equals(reqNeedChkData) || "true".equalsIgnoreCase(reqNeedChkData)) {
            needChkData = true;
        } else {
            needChkData = false;
        }
        String[] directRights = xmlReader.getSimpleTextNotNull("DirectRight").split(",");
        directRight = new HashSet<String>(Arrays.asList(directRights));
    }

    public ReqQryReceipt(ReqRespHeader p_header) {
        reqHeader = p_header;
    }

    public Date getDateFrom() throws ParseException {
        if (dateFrom.length() == 8) {
            return Format.Date8Format.parse(dateFrom);
        } else {
            return Format.Date10Format.parse(dateFrom);
        }

    }

    public Date getDateTo() throws ParseException {
        if (dateTo.length() == 8) {
            return Format.Date8Format.parse(dateTo);
        } else {
            return Format.Date10Format.parse(dateTo);
        }
    }

    public boolean chkBusDate() throws Exception {
        int intToday = Integer.parseInt(Format.Date8Format.format(getDateTo()));
        int intBusDate = Integer.parseInt(Format.Date8Format.format(getDateFrom()));
        if (intToday > intBusDate) {
            return true;
        } else {
            return false;
        }
    }
}
